package com.core.zyyz.amc.modules.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import jodd.util.StringUtil;
import org.apache.commons.collections4.MapUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.core.common.annotation.Log;
import com.core.common.core.controller.BaseController;
import com.core.common.core.domain.AjaxResult;
import com.core.common.enums.BusinessType;
import com.core.zyyz.amc.modules.domain.AmcBusinessCategories;
import com.core.zyyz.amc.modules.service.AmcBusinessCategoriesService;
import com.core.common.core.page.TableDataInfo;

/**
 * 项目类型维护Controller
 *
 * @author LiuFushun
 * @date 2024-11-04
 */
@RestController
@RequestMapping("/categories")
public class AmcBusinessCategoriesController extends BaseController
{
    @Autowired
    private AmcBusinessCategoriesService amcBusinessCategoriesService;

    /**
     * 查询项目类型维护列表
     */
    @PreAuthorize("@ss.hasPermi('amc:categories:list')")
    @GetMapping("/list")
    public TableDataInfo list(AmcBusinessCategories amcBusinessCategories) {
        startPage();
        List<AmcBusinessCategories> list = amcBusinessCategoriesService.selectAmcBusinessCategoriesList(amcBusinessCategories);
        return getDataTable(list);
    }

    /**
     * 业务类型父节点列表
     */
    @PreAuthorize("@ss.hasPermi('amc:categories:list')")
    @GetMapping("/parentList")
    public TableDataInfo parentList(AmcBusinessCategories amcBusinessCategories) {
        startPage();
        amcBusinessCategories.setIsLeaf("false");
        List<AmcBusinessCategories> list = amcBusinessCategoriesService.selectAmcBusinessCategoriesList(amcBusinessCategories);
        return getDataTable(list);
    }


    /**
     * 获取项目类型维护详细信息
     */
    @PreAuthorize("@ss.hasPermi('amc:categories:query')")
    @GetMapping(value = "/{bizId}")
    public AjaxResult getInfo(@PathVariable("bizId") String bizId)
    {
        return success(amcBusinessCategoriesService.selectAmcBusinessCategoriesByBizId(bizId));
    }

    /**
     * 新增项目类型维护
     */
    @PreAuthorize("@ss.hasPermi('amc:categories:add')")
    @Log(title = "项目类型维护", businessType = BusinessType.INSERT)
    @PostMapping("/insertCategories")
    @Transactional
    public AjaxResult add(@RequestBody AmcBusinessCategories amcBusinessCategories)
    {
        return toAjax(amcBusinessCategoriesService.insertAmcBusinessCategories(amcBusinessCategories));
    }

    /**
     * 修改项目类型维护
     */
    @PreAuthorize("@ss.hasPermi('amc:categories:edit')")
    @Log(title = "项目类型维护", businessType = BusinessType.UPDATE)
    @PostMapping("/updateCategories")
    @Transactional
    public AjaxResult edit(@RequestBody AmcBusinessCategories amcBusinessCategories)
    {
        return toAjax(amcBusinessCategoriesService.updateAmcBusinessCategories(amcBusinessCategories));
    }

    /**
     * 删除项目类型维护
     */
    @PreAuthorize("@ss.hasPermi('amc:categories:remove')")
    @Log(title = "项目类型维护", businessType = BusinessType.DELETE)
    @PostMapping("/{bizIds}")
    public AjaxResult remove(@PathVariable String[] bizIds)
    {
        return toAjax(amcBusinessCategoriesService.deleteAmcBusinessCategoriesByBizIds(bizIds));
    }


    /**
     * 查询项目节点定义
     */
    @GetMapping("/query/projectNode")
    public TableDataInfo queryCategoriesNode(@RequestParam String bizId) {
        List<HashMap<String ,Object>> list = amcBusinessCategoriesService.queryCategoriesMapping(bizId);
        return getDataTable(list);
    }

    /**
     * 映射关联保存
     * @param param
     * @return
     */
    @PostMapping("/addMapping")
    @Transactional
    public AjaxResult addMapping(@RequestBody Map<String ,Object> param) {
        String bizId = MapUtils.getString(param,"bizId");
        if(StringUtil.isNotEmpty(bizId)){
            List<JSONObject> updateList = (List<JSONObject>) param.get("updateList");
            if(updateList.size()>0){
                updateList.stream().forEach(i->{
                    Map valuep = JSONObject.toJavaObject(i, Map.class);
                    String runStatus = valuep.get("runStatus")+"";
                    if("1".equals(runStatus)){//启用
                        valuep.put("bizId",bizId);
                        int count = amcBusinessCategoriesService.getMappingCount(valuep);
                        if(count == 0){
                            amcBusinessCategoriesService.addCategoriesNode(valuep);
                        }
                    }else{
                        HashMap<String,Object> hashMap = new HashMap<String,Object>();
                        String subnodeId = valuep.get("subnodeId")+"";
                        hashMap.put("bizId",bizId);
                        hashMap.put("subnodeId",subnodeId);
                        amcBusinessCategoriesService.deleteCategoriesNodeById(hashMap);
                    }
                });
            }
        }
        return success();
    }
}
